#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

// int open(const char *pathname, int flags, /* mode_t mode */ );

int main()
{

    int fd; // file descriptor 文件描述符
    fd = open("./test.txt", O_RDONLY);
    // fd = open("./test22.txt", O_RDONLY | O_CREAT);
    // fd = open("./test.txt", O_RDONLY | O_CREAT | O_EXCL);
    // fd = open("/home/gec/test/test.txt", O_RDONLY | O_CREAT, 0644);
    // -rw-r--r--  1 gec gec     0  8月 15 10:31 test.txt

    if (fd == -1)
    {
        printf("open file fail\n");
        return -1;
    }
    printf("open file success fd=%d\n", fd);
    // open file success fd=3

    int fd2;
    fd2 = open("./test22.txt", O_RDONLY);
    printf("open file success fd2=%d\n", fd2);

    close(fd);
    close(fd2);
    close(fd2);
    close(fd2);
    close(fd2);
    close(fd2);
    printf("close file success\n");
    printf("open file success fd2=%d\n", fd2);

    fd2 = open("./test.txt", O_RDONLY);
    printf("open file success fd2=%d\n", fd2);

    return 0;
}

/* int main()
{

    while (1)
    {
        int fd;
        fd = open("./test.txt", O_RDONLY);

        if (fd == -1)
        {
            printf("open file fail\n");
            return -1;
        }
        printf("open file success fd=%d\n", fd);
        usleep(1000);
    }
    //  第一次打开 3-1023  范围0-1023   012被占用 标准输入STDIN_FILENO键盘    标准输出STDOUT_FILENO（终端）   标准出错STDERR_FILENO(终端)

    return 0;
} */